﻿using System.Collections.Generic;
using JQMergeFramework;
using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// 实现镂空效果的Mask组件
/// </summary>
public class RadarMap : Graphic
{
    [SerializeField] private RectTransform _target;

    private Vector3 _targetMin = Vector3.zero;
    private Vector3 _targetMax = Vector3.zero;

    private bool _canRefresh = true;
    private Transform _cacheTrans = null;
    public List<Transform> pos = new List<Transform>();

    private float offetY = 0;

    
    protected override void OnPopulateMesh(VertexHelper vh)
    {
        
        vh.Clear();

        // 填充顶点
        UIVertex vert = UIVertex.simpleVert;
        vert.color = color;

        Vector2 selfPiovt = rectTransform.pivot;
        Rect selfRect = rectTransform.rect;
        float outerLx = -selfPiovt.x * selfRect.width;
        float outerBy = -selfPiovt.y * selfRect.height;
        float outerRx = (1 - selfPiovt.x) * selfRect.width;
        float outerTy = (1 - selfPiovt.y) * selfRect.height;
        // 0 - Outer:LT
        vert.position = new Vector3(outerLx, outerTy);
        vh.AddVert(vert);
        // 1 - Outer:RT
        vert.position = new Vector3(outerRx, outerTy);
        vh.AddVert(vert);
        // 2 - Outer:RB
        vert.position = new Vector3(outerRx, outerBy);
        vh.AddVert(vert);
        // 3 - Outer:LB
        vert.position = new Vector3(outerLx, outerBy);
        vh.AddVert(vert);

        //内部8个点
        //顶点信息
        Vector3[] vertices = new Vector3[pos.Count];
        float pre_rad = Mathf.Deg2Rad * 360 / pos.Count;
        for (int i = 0; i < pos.Count; i++) {
            vert.position = pos[i].localPosition - new Vector3(0,offetY,0);
            vh.AddVert(vert);
        }

        
        //0                 1
        //       5   6
        //    4         7 
        //
        //    11         8
        //       10  9
        // 3               2
        
        
        vh.AddTriangle(5, 0,1);
        
        vh.AddTriangle(5, 1,6);
        vh.AddTriangle(6, 1,7);
        
        vh.AddTriangle(7, 1,2);

        vh.AddTriangle(7, 2,8);
        
        vh.AddTriangle(8, 2,9);
        vh.AddTriangle(9, 2,3);
        
        vh.AddTriangle(10, 9,3);
        vh.AddTriangle(10, 3,11);
        
        vh.AddTriangle(11, 3,0);
        vh.AddTriangle(11, 0,4);
        
        vh.AddTriangle(0, 5,4);
    }
    

    protected override void Awake()
    {
        base.Awake();
        _cacheTrans = GetComponent<RectTransform>();
        offetY = GameCameraControl.I.fitDes / 2;
    }

    
    void DrawPolygon(){
        int circle_count = 8;
        Vector3[] vertices = new Vector3[circle_count + 1];
  
        float pre_rad = (Mathf.Deg2Rad * 360 / circle_count);
        for (int i = 0; i < circle_count; i++) {
            float deg = -i * pre_rad;
            float x = Mathf.Cos (deg);
            float y = Mathf.Sin (deg);
            vertices [i + 1] = new Vector3 (x, y, 0) *100;
            GameObject go =   new GameObject("go");
            go.transform.SetParent(this.transform);
            go.transform.localPosition =  new Vector3 (x, y, 0) *100;
            go.name = i.ToString();
        }
        
        
        int[] triangles = new int[circle_count * 3];
        for (int i = 0; i < triangles.Length; i+=3) {
            int first = 0;
            int second = i / 3 + 1;
            int third = second + 1;
            if (third > circle_count) {
                third = 1;
            }
            triangles [i] = first;
            triangles [i + 1] = second;
            triangles [i + 2] = third;
        }
    }

}